package com.lineage.data.item_etcitem;

import com.lineage.config.ConfigOther;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.data.item_etcitem.allcall.AllCall;
import com.lineage.server.datatables.MapsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1CastleLocation;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Party;
import com.lineage.server.serverpackets.S_PacketBoxGree;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class AllCall_party extends AllCall {
  private int type = 0;
  
  public static ItemExecutor get() {
    return (ItemExecutor)new AllCall_party();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    L1Party party = pc.getParty();
    if (!pc.isInParty()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("尚未有组队无法使用此物品"));
      return;
    } 
    if (L1CastleLocation.checkInAllWarArea(pc.getX(), pc.getY(), pc.getMapId())) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("旗子内禁止使用"));
      return;
    } 
    if (!pc.getMap().isPartyPc()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("所在地图无法进行传送"));
      return;
    } 
    if (checkPcHasSkillEffect(pc).booleanValue())
      return; 
    ConcurrentHashMap<Integer, L1PcInstance> pcs = party.partyUsers();
    if (pcs.isEmpty())
      return; 
    if (pcs.size() <= 0)
      return; 
    if (!pc.isInParty())
      return; 
    if (this.type == 1) {
      List<L1PcInstance> players = World.get().getRecognizePlayer((L1Object)pc);
      for (L1PcInstance otherPlayer : players) {
        if (otherPlayer.getClanid() != pc.getClanid()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("有其他血盟成员，无法使用穿云箭"));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("请到没有人的地方使用"));
          return;
        } 
      } 
    } 
    if (this.type == 2) {
      List<L1PcInstance> players = World.get().getRecognizePlayer((L1Object)pc);
      for (L1PcInstance otherPlayer : players) {
        if (otherPlayer.getClanid() != 0 && 
          otherPlayer.getClanid() != pc.getClanid()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("有其他血盟成员，无法使用穿云箭"));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("请到没有人的地方使用"));
          return;
        } 
      } 
    } 
    if (!pc.getInventory().checkItem(ConfigOther.Call_party_itemid, ConfigOther.Call_party_count)) {
      pc.sendPacketsAll((ServerBasePacket)new S_SystemMessage(ConfigOther.clanmsg5));
      return;
    } 
    pc.getInventory().consumeItem(ConfigOther.Call_party_itemid, ConfigOther.Call_party_count);
    pc.sendPackets((ServerBasePacket)new S_SkillSound(pc.getId(), 2047));
    Iterator<L1PcInstance> iterator = pcs.values().iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc2 = iterator.next();
      if (!pc.isInParty() || !pc.getParty().isMember(pc2) || 
        pc2.getId() == pc.getId())
        continue; 
      if (checkPcHasSkillEffect(pc2).booleanValue())
        continue; 
      pc2.setcallclanal(pc.getId());
      teleport_AllCall((L1Character)pc2, (L1Character)pc, 2);
    } 
    World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage(
          String.format(ConfigOther.partymsg, new Object[] { pc.getName(), MapsTable.get().getMapName(pc.getMapId()), item.getItem().getName() })));
    World.get().broadcastPacketToAll((ServerBasePacket)new S_PacketBoxGree(2, 
          String.format(ConfigOther.partymsg, new Object[] { pc.getName(), MapsTable.get().getMapName(pc.getMapId()), item.getItem().getName() })));
  }
  
  public void set_set(String[] set) {
    try {
      this.type = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
  }
}
